<?php
class PriceAction extends BaseAction{
	
		public function index() {
			$partner_id = $_REQUEST['partner_id'];
			$goods_id = $_REQUEST['goods_id'];
			$enable = $_REQUEST['enable'];
			if(empty($enable)){
				$enable='all';
			}
			if(!empty($goods_id)){
				$map['goods_id'] = $goods_id;
			}else{
				if(!empty($partner_id)){
					$map1['pid'] = $partner_id;
					$gids_ = D('Goods')->selectGoodsInfo($map1,'id');
					foreach($gids_ as $vo1){$gids[]=$vo1['id'];}
					$map['goods_id'] = array('in',$gids);
				}
			}
			if($enable!='all'){
				$map['enable']= $enable;
			}
			$pagedata = D('GoodsPrice')->selectPrices($map);
			$this->assignRequestParams();
			$this->assign('pagedata', $pagedata);
			$this->display();
		}
	
	
		public function addPrice() {
				$this->display('add');
		}
	
		
		public function doAddPrice() {
				$map['goods_id'] 			= intval( $_POST['goods_id'] );
				$map['in_price'] 			= floatval( $_POST['in_price'] );
				$map['market_price'] 	= floatval( $_POST['market_price'] );
				$map['total_number'] 	= intval( $_POST['total_number'] );
				
				$map['expire_time'] = paramDate( $_POST['expire_time'] );
				$map['start_time'] 	= paramDate( $_POST['start_time']  );
				
				if(isset($_POST['enable'])) $map['enable'] = 'Y'; else $map['enable'] = 'N';
				
				//参数合法性检查
				$required_field = array(
						'goods_id'		=> '商品',
						'expire_time'	=> '截止日',
						'start_time'	=> '开始日',
						'total_number'=> '总量',
						//'goods_id'		=> '商品'
				);
				foreach ($required_field as $k => $v) {
						if ( empty($map[$k]) ){
							 	//exit(json_encode(array('status'=>'-1','title'=>'','msg'=>$v.'不可为空')));
							 	$this->error($v . '不可为空');
						}
				}
				
				if($map['expire_time'] <= $map['start_time']){
						//exit(json_encode(array('status'=>'-1','title'=>'','msg'=>'截止日期需大于开始日期')));
						$this->error('截止日期需大于开始日期');
				}
				
				
		    $map['ctime'] 			=	time();
		    $map['is_delete'] 	= 'N';
		    $map['insert_admin_id']	=	$this->adminId;
		    
		    $priceDao = D('GoodsPrice');
		    
		    $checkresult = $priceDao->checkPrice($map);
		    if(!$checkresult)
		    		//exit(json_encode(array('status'=>'-1','title'=>'','msg'=>'新增价格数据检查未通过！')));
		    		$this->error('新增价格数据检查未通过！');
		    
		    $id = $priceDao->add($map);
		    //unset($map);
		    if(!empty($id)){
						$this->success('新增成功！');
						//exit(json_encode(array('status'=>'1','title'=>'','msg'=>'新增成功！')));
				}else{
					
				}
				//exit(json_encode(array('status'=>'0','title'=>'','msg'=>'新增失败！')));
				$this->error('新增失败！');
		}
	
	
		public function editPrice() {
			$priceId = $_GET['priceId'];
			if(empty($priceId))$this->error('参数错误！');
			$price = D('GoodsPrice')->selectById($priceId);
			if(empty($price))$this->error('商户不存在！');
			$this->assign('type', 'edit');
			$this->assign('price', $price);
			$this->display('edit');
		}
	
		public function doEditPrice() {
				$map['id'] 			= intval( $_POST['priceId'] );
				$map['goods_id'] 			= intval( $_POST['goods_id'] );
				$map['in_price'] 			= floatval( $_POST['in_price'] );
				$map['market_price'] 	= floatval( $_POST['market_price'] );
				$map['total_number'] 	= intval( $_POST['total_number'] );
				
				$map['expire_time'] = paramDate( $_POST['expire_time'] );
				$map['start_time'] 	= paramDate( $_POST['start_time']  );
				
				if($_POST['enable']=='Y') $map['enable'] = 'Y'; else $map['enable'] = 'N';
				
				//参数合法性检查
				$required_field = array(
						'id'					=>	'价格ID',
						'goods_id'		=> '商品',
						'expire_time'	=> '截止日',
						'start_time'	=> '开始日',
						'total_number'=> '总量',
						//'goods_id'		=> '商品'
				);
				foreach ($required_field as $k => $v) {
						if ( empty($map[$k]) ){
							 	exit(json_encode(array('status'=>'-1','title'=>'','msg'=> $v.'不可为空')));
							 	//$this->error($v . '不可为空');
						}
				}
				
				if($map['expire_time'] <= $map['start_time']){
						exit(json_encode(array('status'=>'-1','title'=>'','msg'=>'截止日期需大于开始日期')));
				}

		    $map['mtime'] 			=	time();
		    
		    $priceDao = D('GoodsPrice');
		    
		    $checkresult = $priceDao->checkPrice($map);
		    if(!$checkresult)
		    		exit(json_encode(array('status'=>'-1','title'=>'','msg'=>'更新价格数据检查未通过！')));
		    
		    $id = $priceDao->save($map);
		    //unset($map);
		    if(!empty($id)){
						//$this->success('新增成功！');
						exit(json_encode(array('status'=>'1','title'=>'','msg'=>'更新成功！')));
				}else{
					
				}
				exit(json_encode(array('status'=>'0','title'=>'','msg'=>'更新失败！')));
				//$this->error('新增失败！');
		}
	
		public function doDeletePrice() {
			$ary_ids	= explode(',', t($_POST['ids']));
			if (empty($ary_ids)) {
				echo 0;
				return ;
			}
			echo D('GoodsPrice')->deleteByIds($ary_ids) ? '1' : '0';
		}
		
		/**
		*	获取商品当前价格，返回价格数据（json格式），返回空则说明无对应数据。
		*	暂时没用上。因考虑到市场价和总量应时时查询。
		**/
		public function getAvailabePrice(){
				$goods_id = intval($_REQUEST['goods_id']);
				if (empty($goods_id)) exit(json_encode(array()));
				$price_ = D('GoodsPrice')->selectAvailabePriceByGoodsId($goods_id);
				foreach($price_ as $vo){
						$vo['expire_time'] = paramMillisecond2Date($vo['expire_time'],'Y-m-d','');
						$vo['start_time'] = paramMillisecond2Date($vo['start_time'],'Y-m-d','');
						$price[] = $vo;
				}
				exit(json_encode($price));
		}
	
}
?>